// pages/index/personage/order/order.js 
const app = getApp()
const connector = require('../../../utils/connector.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    perindex: 100,
    bar_Height: wx.getSystemInfoSync().statusBarHeight + 45,
    font:'正在加载...',
    statuss:'',
    page:1,//页数
    last: 0,
    orderAll:[

    ]
  },
  /**进入订单详情 */
  orderOne(e) {
    var that = this
    var index = e.currentTarget.dataset.id
    // //将对象转为string
    var queryBean = JSON.stringify(index)
    wx.navigateTo({
      url: '/pages/oneself/orderList/details/details?queryBean=' + queryBean,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      perindex: 100,
      // content: '',
    })
  },
  /** 订单列表 */
  perIPayment: function(e) {
    let that = this
    // that.setData({
    //   perindex: e.currentTarget.dataset.current
    // })

    
    let perindex = e.currentTarget.dataset.current
    let statuss = ''
    if (perindex == 100){
      statuss = ''
    }else{
      statuss = perindex
    }
    that.setData({
      last: 0,
      page: 1,
      statuss: statuss,
      perindex: perindex
    })
    sundryOrderList(this, 1, statuss)
    

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    sundryOrderList(this, 1, "")
    this.setData({
      statuss:""
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    wx.hideShareMenu()
    // conversation.conversation(that)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //模拟加载
    setTimeout(function () {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1500);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this
    const page = that.data.page + 1
    sundryOrderList(this, page, that.data.statuss)
    that.setData({
      page: page
    })


  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})
//获取订单列表
function sundryOrderList(that, page, statuss) {
  const pages = {
    page: page,
    status: statuss
  }
  let orderAll = that.data.orderAll
  let font = "", last = that.data.last;
  
  if (last == 0){
    that.setData({
      font: "正在加载"
    })
    connector.getFunc(connector.connector().sundryOrderList, pages, "get", app.tokens, function (res) {

      if (res.statusCode == 200) {
        if (page == 1) {
          orderAll = res.data.data
          if (res.data.data.length < 1) {
            font = "暂无订单"
            last = 1
          } else {
            if (res.data.last_page == res.data.current_page) {
              font = "没有更多了"
              last = 1
            } else {
              font = "加载完成"
              last = 0
            }
          }
        } else {
          if (res.data.current_page <= res.data.last_page){
            
            orderAll = orderAll.concat(res.data.data)
            if (res.data.current_page == res.data.last_page) {
              font = "没有更多了"
              last = 1
            } else {
              font = "加载完成"
              last = 0
            }
          }
        }
        that.setData({
          orderAll: orderAll,
          font: font,
          last: last
        })

      }
    }, function (res) {
      wx.showToast({
        title: "内部错误",
        icon: 'none',
        duration: 1000
      })
    })
  }
  
}